package command.usuario;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import model.Caso;

import command.ICommand;

import dao.assinatura.CasoDAO;
import dao.fabrica.FabricaDAO;

public class EditarCasoCommand implements ICommand{
	
	private Date dataInicio;
	private Date dataFim;
	private String obs;
	private String situacao;
	private String casoId;
	
	public String execute(HttpServletRequest request) throws ServletException, IOException {
			
			try {
				SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
				 situacao = request.getParameter("inputSituacao");
				 casoId = request.getParameter("c");
				 if (situacao != null)
				 {
					 
					 obs = request.getParameter("inputObs");
					 try {
							if (!request.getParameter("inputDataInicio").equals(""))
								dataInicio =  (Date)format.parse(request.getParameter("inputDataInicio"));
							if (!request.getParameter("inputDataFim").equals(""))
								dataFim = (Date)format.parse(request.getParameter("inputDataFim"));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}  
					  
					 Caso caso = new Caso(situacao,dataInicio,dataFim,obs);
					 caso.setId(Integer.parseInt(casoId));
					 CasoDAO casoDAO = FabricaDAO.createCaso();
					 casoDAO.updateCaso(caso);
					 
				 }else
					 return "/paginas/editar/caso.jsp?c="+casoId;
				 
				 
			} catch (SQLException e) {
				e.printStackTrace();
				return "/paginas/cadastro_invalido.jsp";
			}
			
			return "/paginas/editar/caso.jsp?resultado=OK&c="+casoId;
		}
}
